package xin.yangshuai.oauth201.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import xin.yangshuai.common01.entity.AuthorizationInfo;
import xin.yangshuai.oauth201.config.AuthorizationCodeConfig;

import java.util.Map;

@Controller
@RequestMapping("authorizationCode")
public class AuthorizationCodeController {

    public static final Logger logger = LoggerFactory.getLogger(AuthorizationCodeController.class);

    @Autowired
    private AuthorizationCodeConfig authorizationCodeConfig;

    @RequestMapping("redirect")
    public String redirect() {

        String authorizeUrl = authorizationCodeConfig.getAuthorizeUrl();
        String redirectUrl = authorizationCodeConfig.getRedirectUrl();
        String clientId = authorizationCodeConfig.getClientId();

        return "redirect:" + authorizeUrl + "?client_id=" + clientId + "&redirect_uri=" + redirectUrl + "&response_type=code&state=123";
    }

    @RequestMapping("callback")
    @ResponseBody
    public Map callback(String code, String state) {

        String clientId = authorizationCodeConfig.getClientId();
        String clientSecret = authorizationCodeConfig.getClientSecret();
        String accessTokenUrl = authorizationCodeConfig.getAccessTokenUrl();
        String redirectUrl = authorizationCodeConfig.getRedirectUrl();
        String userInfoUri = authorizationCodeConfig.getUserInfoUri();

        logger.info("code=[{}]", code);
        logger.info("state=[{}]", state);

        RestTemplate restTemplate = new RestTemplate();
        String url = accessTokenUrl + "?client_id=" + clientId + "&client_secret=" + clientSecret + "&code=" + code + "&redirect_uri=" + redirectUrl + "&grant_type=authorization_code";
        System.out.println(url);
        AuthorizationInfo authorizationInfo = restTemplate.postForObject(url, null, AuthorizationInfo.class);

        String accessToken = authorizationInfo.getAccessToken();

        Map map = restTemplate.getForObject(userInfoUri + "?access_token=" + accessToken, Map.class);

        return map;
    }

    @RequestMapping("refreshToken")
    @ResponseBody
    public AuthorizationInfo refreshToken(String refreshToken) {

        String clientId = authorizationCodeConfig.getClientId();
        String clientSecret = authorizationCodeConfig.getClientSecret();
        String refreshTokenUrl = authorizationCodeConfig.getRefreshTokenUrl();

        logger.info("refresh_token=[{}]", refreshToken);

        RestTemplate restTemplate = new RestTemplate();
        AuthorizationInfo authorizationInfo = restTemplate.getForObject(refreshTokenUrl + "?clientId=" + clientId + "&clientSecret=" + clientSecret + "&refreshToken=" + refreshToken + "&grantType=refresh_code", AuthorizationInfo.class);

        return authorizationInfo;
    }
}
